import pandas  as pd
import  numpy as np
import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "root", "123456", "test")

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

sql = "SHOW TABLES FROM `test`"

# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
secli = []
df = pd.DataFrame()
print (len(results))
for i in results:
    sql = "select * from `%s`"%(i)
    cursor.execute(sql)
    res = cursor.fetchall()

    tmp1 = []
    tmp2 = []
    if res != ():
        secli.append("sec"+i[0])
        for q  in res:
            tmp1.append(q[0])
            tmp2.append(q[1])
        tmp1 = np.array(tmp1)
        df1 = pd.DataFrame(tmp2)
        df1 = df1.T
        df1.columns = (tmp1)
        df = pd.concat((df,df1),axis=0)
secli = np.array(secli)

df = df.set_index(secli)
df.to_csv('final.csv')
print (df)
